home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / QuickDraw / Imageer 1.0.0d3 / source / doevent.c next >
Encoding:
C/C++ Source or Header  |  1996-02-16  |  11.0 KB  |  398 lines  |  [TEXT/MPS ]

  1. /****************************************************/
  2. /*                                                    */
  3. /*    File:        doevent.c                            */
  4. /*                                                    */
  5. /*    Program:    Imageer                                */
  6. /*                                                    */
  7. /*    By:            Jason Hodges-Harris                    */
  8. /*                                                    */
  9. /*    Created:    26/10/95  00:00:00 AM                */
  10. /*                                                    */
  11. /*    Version:    1.0.0d3                                */
  12. /*                                                    */
  13. /*    Copyright:    © 1995-96 Apple Computer, Inc.,        */ 
  14. /*                    all rights reserved.            */        
  15. /*                                                    */
  16. /****************************************************/
  17.  
  18.  
  19. /**** Macintosh Toolbox Headers *****/
  20.  
  21. #ifndef __CURSORCTL__
  22. #include <CursorCtl.h>
  23. #endif
  24.  
  25. #ifndef __DESK__
  26. #include <Desk.h>
  27. #endif
  28.  
  29. #ifndef __DISKINIT__
  30. #include <DiskInit.h>
  31. #endif
  32.  
  33. #ifndef __EVENTS__
  34. #include <Events.h>
  35. #endif
  36.  
  37. #ifndef __MENUS__
  38. #include <Menus.h>
  39. #endif
  40.  
  41. #ifndef __QDOFFSCREEN__
  42. #include <QDOffscreen.h>
  43. #endif
  44.  
  45. #ifndef __TOOLUTILS__
  46. #include <ToolUtils.h>
  47. #endif
  48.  
  49. #ifndef __WINDOWS__
  50. #include <Windows.h>
  51. #endif
  52.  
  53.  
  54. /****   Application headers and prototypes   ****/
  55.  
  56.  
  57. #ifndef __IMAGEERAPPHEADER__
  58. #include "Imageer.app.h"
  59. #endif
  60.  
  61. #ifndef __IMAGEERPROTOSHEADER__
  62. #include "Imageer.protos.h"
  63. #endif
  64.  
  65.  
  66. // DoEvent function processess the events posted by the application.
  67. // This includes the handling of mouse, key,
  68. // disk and operating system events.
  69.  
  70. #pragma segment Main
  71. void DoEvent (EventRecord *eventPtr)
  72. {
  73.     char            theChar;
  74.     
  75.     switch (eventPtr->what)
  76.     {
  77.         case nullEvent:
  78.         break;
  79.         case mouseDown:
  80.             HandleMouseDown(eventPtr);
  81.         break;
  82.         case mouseUp:
  83.         break;
  84.         case keyDown:
  85.         case autoKey:
  86.             theChar=eventPtr->message & charCodeMask;
  87.              if ((eventPtr->modifiers &cmdKey) != 0)
  88.                  DoMenuCommand(MenuKey (theChar));
  89.         break;
  90.         case keyUp:
  91.         break;
  92.         case updateEvt:
  93.             DoWindowUpdate((WindowPtr)(eventPtr->message));
  94.         break;
  95.         case diskEvt:
  96.             DoDiskEvt(eventPtr);    // disk inserted event
  97.         break;
  98.         case activateEvt:
  99.             DoWindowUpdate((WindowPtr)(eventPtr->message));
  100.         break;
  101.         case osEvt:
  102.             if (eventPtr->message>>24==resumeFlag)
  103.                 SetCursor(&qd.arrow);
  104.         break;
  105.     }
  106. }
  107.  
  108.  
  109. // HandleMouseDown controls the further processing of mouse button down events.
  110. // This includes testing the location of the mouse pointer at the time of the event
  111. // and performing the appropriate action. e.g. if the mouse button was pressed
  112. // whilst the pointer was in the menubar, the value returned by MenuSelect()
  113. // (used to determine which menubar and item selected) is passed into DoMenuCommand()
  114. // which in turn handles the processing of the selected menu items.
  115.  
  116. #pragma segment Main
  117. void HandleMouseDown(EventRecord *eventPtr)
  118. {
  119.     ImageDocHndl        theWindDocHndl;
  120.     MenuHandle            theMenu;
  121.     WindowPtr            theWindow,
  122.                         oldPort;
  123.     long                menuChoice;
  124.     Str255                theResString;
  125.     short                thePart,
  126.                         maxXsize,
  127.                         maxYsize;
  128.     
  129.     theMenu = GetMHandle(mModel);
  130.     thePart=FindWindow (eventPtr->where,&theWindow);
  131.     switch (thePart)
  132.     {
  133.         case inMenuBar:
  134.             menuChoice = MenuSelect (eventPtr->where);
  135.             DoMenuCommand(menuChoice);
  136.         break;
  137.         case inSysWindow:
  138.             SystemClick(eventPtr,theWindow);
  139.         break;
  140.         case inDrag:
  141.             SetCurrentWindowMark(theWindow);
  142.             if (FrontWindow() != theWindow)
  143.             {
  144.                 theWindDocHndl = (ImageDocHndl)GetWRefCon(theWindow);
  145.                 if (!(**theWindDocHndl).isColorsWindow)
  146.                 {
  147.                     SetUndoItemText((*theWindDocHndl)->theUndoState);
  148.                     if (ColorWindowVisible())
  149.                         UpdateColorsWindPalette(theWindDocHndl, theWindow);
  150.                     if ((**theWindDocHndl).isUsingQDGX)
  151.                     {
  152.                         GetIndString (theResString,rImageModel,iUseColorQD);
  153.                         SetMenuItemText (theMenu,iUseQDGX,theResString);
  154.                         SetMenuItemAvailable(true);
  155.                     }
  156.                     else
  157.                     {
  158.                         GetIndString (theResString,rImageModel,iUseQuickDrawGX);
  159.                         SetMenuItemText (theMenu,iUseQDGX,theResString);
  160.                         SetMenuItemAvailable(false);
  161.                     }
  162.                 }
  163.                 else 
  164.                     SetUndoItemText(kCannotUndo);
  165.                 SetCurrentWindowMark(theWindow);
  166.                 SelectWindow(theWindow);
  167.             }
  168.             DragSelWind(theWindow,eventPtr->where);
  169.         break;
  170.         case inGrow:
  171.             HandleWindowReSize(theWindow, eventPtr->where);
  172.         break;
  173.         case inGoAway:
  174.             DoGoAwayWind(theWindow,eventPtr->where);
  175.         break;
  176.         case inContent:
  177.             if (FrontWindow() != theWindow)
  178.             {
  179.                 theWindDocHndl = (ImageDocHndl)GetWRefCon(theWindow);
  180.                 if (!(**theWindDocHndl).isColorsWindow)
  181.                 {
  182.                     SetUndoItemText((*theWindDocHndl)->theUndoState);
  183.                     if (ColorWindowVisible())
  184.                         UpdateColorsWindPalette(theWindDocHndl, theWindow);
  185.                     if ((**theWindDocHndl).isUsingQDGX)
  186.                     {
  187.                         GetIndString (theResString,rImageModel,iUseColorQD);
  188.                         SetMenuItemText (theMenu,iUseQDGX,theResString);
  189.                         SetMenuItemAvailable(true);
  190.                     }
  191.                     else
  192.                     {
  193.                         GetIndString (theResString,rImageModel,iUseQuickDrawGX);
  194.                         SetMenuItemText (theMenu,iUseQDGX,theResString);
  195.                         SetMenuItemAvailable(false);
  196.                     }
  197.                 }
  198.                 else 
  199.                     SetUndoItemText(kCannotUndo);
  200.                 SetCurrentWindowMark(theWindow);
  201.                 SelectWindow(theWindow);
  202.             }
  203.             else
  204.                 ScrollBarHandler(eventPtr->where, theWindow);    // find if current window control selected
  205.         break;
  206.         case inZoomIn:
  207.         case inZoomOut:
  208.             theWindDocHndl = (ImageDocHndl)GetWRefCon((WindowPtr)theWindow);
  209.             MoveWindow((WindowPtr)theWindow,0,18+GetMBarHeight(),true);
  210.             SizeWindow((WindowPtr)theWindow,
  211.             (**theWindDocHndl).theMaxWindowSize.right - (**theWindDocHndl).theMaxWindowSize.left,
  212.             (**theWindDocHndl).theMaxWindowSize.bottom - (**theWindDocHndl).theMaxWindowSize.top,true);
  213.             MoveControl((**theWindDocHndl).theVScrollBar,
  214.                         theWindow->portRect.right - kScrollBarWidth,theWindow->portRect.top - 1);
  215.             MoveControl((**theWindDocHndl).theHScrollBar,
  216.                         theWindow->portRect.left - 1,theWindow->portRect.bottom - kScrollBarWidth);
  217.             SizeControl((**theWindDocHndl).theVScrollBar,16,
  218.                         (theWindow->portRect.bottom - theWindow->portRect.top)-13);
  219.             SizeControl((**theWindDocHndl).theHScrollBar,
  220.                         (theWindow->portRect.right - theWindow->portRect.left)-13,16);
  221.             SetControlMinimum((**theWindDocHndl).theVScrollBar,0);
  222.             SetControlMinimum((**theWindDocHndl).theHScrollBar,0);
  223.             maxXsize = (**theWindDocHndl).theImageXSize - 
  224.                         ((theWindow->portRect.right - kScrollBarWidth) - theWindow->portRect.left);
  225.             maxYsize = (**theWindDocHndl).theImageYSize - 
  226.                         ((theWindow->portRect.bottom - kScrollBarWidth) - theWindow->portRect.top);
  227.             SetControlMaximum((**theWindDocHndl).theVScrollBar,maxYsize);
  228.             SetControlMaximum((**theWindDocHndl).theHScrollBar,maxXsize);
  229.             SetControlValue((**theWindDocHndl).theVScrollBar,0);
  230.             SetControlValue((**theWindDocHndl).theHScrollBar,0);
  231.             GetPort(&oldPort);
  232.             SetPort(theWindow);
  233.             InvalRect(&theWindow->portRect);
  234.             SetPort(oldPort);
  235.         break;
  236.     }
  237.     DrawMenuBar();
  238. }
  239.  
  240.  
  241. // DoDiskEvt handles the event post by the OS when a disk is insert
  242. // and displays an error is the media cannot be mounted.
  243.  
  244. #pragma segment Main
  245. void DoDiskEvt(EventRecord *eventPtr)
  246. {
  247.     short    ErrResult;
  248.     Point    ErrPoint;
  249.  
  250.     if((HiWord(eventPtr->message)!=noErr))
  251.     {
  252.         SetPt(&ErrPoint,100,100);
  253.         ErrResult=DIBadMount(ErrPoint,eventPtr->message);
  254.     }
  255. }
  256.  
  257.  
  258. /****    Handle image window scroll bars        ****/
  259.  
  260. // Handle the actions of the scroll bars in the image windows,
  261. // using control actions to track controls when the mouse button is held down.
  262.  
  263. #pragma segment Main
  264. OSErr    ScrollBarHandler(Point    Position, WindowPtr theWindow)
  265. {
  266.     ImageDocHndl         theDocHndl;
  267.     ControlHandle        theScrollBar;
  268.     ControlActionUPP    theVertActionScrollUPP,
  269.                         theHorizActionScrollUPP;
  270.     WindowPtr            oldPort;
  271.     short                theControlPart;
  272.     OSErr                error = noErr;
  273.     
  274.     GetPort(&oldPort);
  275.     SetPort(theWindow);
  276.     GlobalToLocal(&Position);
  277.     theDocHndl = (ImageDocHndl)GetWRefCon(theWindow);
  278.     theControlPart = FindControl(Position, theWindow, &theScrollBar);
  279.     theVertActionScrollUPP = NewControlActionProc(ImageWindVertAction);
  280.     theHorizActionScrollUPP = NewControlActionProc(ImageWindHorizAction);
  281.     switch (theControlPart)
  282.     {
  283.         case inUpButton:
  284.             if (theScrollBar == (**theDocHndl).theVScrollBar)
  285.                 TrackControl(theScrollBar,Position,theVertActionScrollUPP);
  286.             else if (theScrollBar == (**theDocHndl).theHScrollBar)
  287.                 TrackControl(theScrollBar,Position,theHorizActionScrollUPP);
  288.         break;
  289.         case inDownButton:
  290.             if (theScrollBar == (**theDocHndl).theVScrollBar)
  291.                 TrackControl(theScrollBar,Position,theVertActionScrollUPP);
  292.             else if (theScrollBar == (**theDocHndl).theHScrollBar)
  293.                 TrackControl(theScrollBar,Position,theHorizActionScrollUPP);
  294.         break;
  295.         case inPageUp:
  296.             if (theScrollBar == (**theDocHndl).theVScrollBar)
  297.                 TrackControl(theScrollBar,Position,theVertActionScrollUPP);
  298.             else if (theScrollBar == (**theDocHndl).theHScrollBar)
  299.                 TrackControl(theScrollBar,Position,theHorizActionScrollUPP);
  300.         break;
  301.         case inPageDown:
  302.             if (theScrollBar == (**theDocHndl).theVScrollBar)
  303.                 TrackControl(theScrollBar,Position,theVertActionScrollUPP);
  304.             else if (theScrollBar == (**theDocHndl).theHScrollBar)
  305.                 TrackControl(theScrollBar,Position,theHorizActionScrollUPP);
  306.         break;
  307.         case inThumb:
  308.             if (theScrollBar == (**theDocHndl).theVScrollBar)
  309.             {
  310.                 theControlPart = TrackControl(theScrollBar,Position,nil);
  311.                 if (theControlPart)
  312.                 {
  313.                     InvalRect(&theWindow->portRect);
  314.                 }
  315.             }
  316.             else if (theScrollBar == (**theDocHndl).theHScrollBar)
  317.             {
  318.                 theControlPart = TrackControl(theScrollBar,Position,nil);
  319.                 if (theControlPart)
  320.                 {
  321.                     InvalRect(&theWindow->portRect);
  322.                 }
  323.             }
  324.         break;
  325.     }
  326.     DisposeRoutineDescriptor((UniversalProcPtr)theVertActionScrollUPP);
  327.     DisposeRoutineDescriptor((UniversalProcPtr)theHorizActionScrollUPP);
  328.     SetPort(oldPort);
  329.     return error;
  330. }
  331.  
  332.  
  333. /****        Update image window scroll bar controls    ****/
  334.  
  335. #pragma segment Main
  336. void    UpdateImageWindCntrl(ControlHandle theControlHndl, WindowPtr theWindow, short stepSize)
  337. {
  338.     short            theControlValue,
  339.                     theControlBounds;
  340.  
  341.     theControlValue = GetControlValue(theControlHndl);
  342.     theControlBounds = GetControlMaximum(theControlHndl);
  343.     theControlValue = (theControlValue + stepSize > theControlBounds) ?
  344.         theControlBounds : theControlValue + stepSize;
  345.     SetControlValue(theControlHndl,theControlValue);
  346.     InvalRect(&theWindow->portRect);
  347. }
  348.  
  349. /****    Vertical scrolling action function    ****/
  350.  
  351. #pragma segment Main
  352. pascal void ImageWindVertAction(ControlHandle theControl, short theControlPart)
  353. {
  354.     WindowPtr        theWindow;
  355.     
  356.     theWindow = (*theControl)->contrlOwner;
  357.     switch (theControlPart)
  358.     {
  359.         case inUpButton:
  360.             UpdateImageWindCntrl(theControl, theWindow, -kSmallScroller);
  361.         break;
  362.         case inDownButton:
  363.             UpdateImageWindCntrl(theControl, theWindow, kSmallScroller);
  364.         break;        
  365.         case inPageUp:
  366.             UpdateImageWindCntrl(theControl, theWindow, -kLargeScroller);
  367.         break;
  368.         case inPageDown:
  369.             UpdateImageWindCntrl(theControl, theWindow, kLargeScroller);
  370.         break;
  371.     }
  372. }
  373.  
  374.  
  375. /****    Horizontal scrolling action function    ****/
  376.  
  377. #pragma segment Main
  378. pascal void ImageWindHorizAction(ControlHandle theControl, short theControlPart)
  379. {
  380.     WindowPtr        theWindow;
  381.     
  382.     theWindow = (*theControl)->contrlOwner;
  383.     switch (theControlPart)
  384.     {
  385.         case inUpButton:
  386.             UpdateImageWindCntrl(theControl, theWindow, -kSmallScroller);
  387.         break;
  388.         case inDownButton:
  389.             UpdateImageWindCntrl(theControl, theWindow, kSmallScroller);
  390.         break;        
  391.         case inPageUp:
  392.             UpdateImageWindCntrl(theControl, theWindow, -kLargeScroller);
  393.         break;
  394.         case inPageDown:
  395.             UpdateImageWindCntrl(theControl, theWindow, kLargeScroller);
  396.         break;
  397.     }
  398. }